package com.le.tester.onjava8.l_2_3;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * createTime：2023/2/1 16:25
 * description：复制构造器
 */

class FruitQualities {
    private int weight;
    private int color;
    private int firmness;
    private int ripeness;
    private int smell;

    FruitQualities() {
    }

    //复制构造器


    FruitQualities(FruitQualities f) {
        weight = f.weight;
        color = f.color;
        firmness = f.firmness;
        ripeness = f.ripeness;
        smell = f.smell;
    }
}

class Seed {
    Seed() {
    }

    Seed(Seed s) {
    }


}

class Fruit {

    private FruitQualities fq;

    private int seeds;

    private Seed[] s;

    Fruit(FruitQualities q, int seedCount) {

        fq = q;

        seeds = seedCount;

        s = new Seed[seeds];

        for (int i = 0; i < seeds; i++) {
            s[i] = new Seed();
        }
    }

    //复制构造器
    Fruit(Fruit f) {
        fq = new FruitQualities(f.fq);
        seeds = f.seeds;
        s = new Seed[seeds];
        //调用所有的Seed复制构造器

        for (int i = 0; i < seeds; i++) {
            s[i] = new Seed(f.s[i]);
        }
    }

    protected void addQualities(FruitQualities q) {
        fq = q;
    }

    protected FruitQualities getQualities() {
        return fq;
    }
}

class Tomato extends Fruit {

    Tomato() {
        super(new FruitQualities(), 100);
    }

    Tomato(Tomato t) {
        super(t);
    }
}

class ZebraQualities extends FruitQualities {
    private int stripedness;

    //无参构造器
    ZebraQualities() {
        super();
    }

    ZebraQualities(ZebraQualities z) {
        super(z);
        stripedness = z.stripedness;
    }
}

class GreenZebra extends Tomato {
    GreenZebra() {
        addQualities(new ZebraQualities());
    }

    GreenZebra(GreenZebra g) {
        super(g);
        addQualities(new ZebraQualities());

    }

    public void evaluate() {
        ZebraQualities zq = (ZebraQualities) getQualities();
    }
}

public class CopyConstructor {
    public static void ripen(Tomato t) {
        t = new Tomato(t);
        System.out.println("In ripen,t is a " + t.getClass().getName());
    }

    public static void slice(Fruit f) {
        f = new Fruit(f);
        System.out.println("In slice, f is a " + f.getClass().getName());
    }

    public static void ripen2(Tomato t) {
        try {
            Class c = t.getClass();
            Constructor ct = c.getConstructor(new Class[]{c});
            Object obj = ct.newInstance(new Object[]{t});
            System.out.println("In ripen2,t is a " + obj.getClass().getName());
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
            System.out.println(e);
        }
    }

    public static void slice2(Fruit f) {
        try {
            Class c = f.getClass();
            Constructor ct = c.getConstructor(new Class[]{c});
            Object obj = ct.newInstance(new Object[]{f});
            System.out.println("In slice2,f is a " + obj.getClass().getName());
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {

    }

}
